<?php
/**
* OneToMany.php
*
* Contains OneToMany class definition
* @author  John Snook <jsnook@gmail.com>
* @version 2
* @copyright (c) Snook 2004
* @package QuickDataFiles
*/

/**
* OneToMany
*
* This class subclasses Simple to provides one-to-many lookups for display and drop down lists for editing.  You must provide a 
* lookup table and field information for it to work.
* @author  John Snook <jsnook@gmail.com>
* @version 2
* @copyright (c) Snook 2004
* @example /var/www/corp/IntraDox/Documents.php
* @todo Convert to PEAR::DB
* @package QuickData
* @subpackage QuickDataFields
*/
class OneToMany extends Simple
{
	/**
	* Name of the foreign table we will display a list from.
	* @access public
	* @var string
	*/
	var $ForeignTable;
	/**
	* Name of the foreign key we will associate a value with.
	* @access public
	* @var string
	*/
	var $ForeignKeyField;
	/**
	* Name of the foreign data we will display.
	* @access public
	* @var string
	*/
	var $ForeignDataField;
	/**
	* If you want to specify another restriction on the list, set $ParentKeyName and $ParentKeyValue
	* @access public
	* @var string
	*/
	var $ParentKeyName = NULL;
	/**
	* If you want to specify another restriction on the list, set $ParentKeyName and $ParentKeyValue
	* @access public
	* @var string
	*/
	var $ParentKeyValue = NULL;
	/**
	* If you want the first option of the list to be a null option, set this to TRUE
	* @access public
	* @var boolean
	*/
	var $IncludeNone = TRUE;

	/**
	* This allows the field to have a default value on add.
	* @access public
	* @var boolean
	*/
	var $DefaultValue = NULL;
	
	var $CanEdit = TRUE;
	var $CanAdd = TRUE;
	var $InheritParent = NULL;
	/**
	* Create an instance of a OneToMany 
	* expects: $Caption, $LocalField, $ForeignTable, $ForeignKeyField, $ForeignDataField
	* @todo Add list box options.
	* @param string $Caption Duh.
	* @param string $LocalTable The local table to get our primary key from.
	* @param string $LocalField The name of the local key field.
	* @param string $ForeignTable The foreign table name.
	* @param string $ForeignKeyField The foreign key field name.
	* @param string $ForeignDataField The name of the field that has foreign data that we're going to display.
	*/
	function OneToMany($Properties) 
	{
		parent::Simple($Properties);
	}
	
	function GetSQLReadyValue()
	{
		$value = (is_null($this->Value) ? "null" : $this->Value);
		return $value;
	}

	/**
	* Updates the localfield to be equal to whatever the user selected.
	*/
	function Update()
	{
		$value = $this->GetSQLReadyValue();
		$SQL = "UPDATE " . $this->Parent->Parent->DBTableName . " SET $this->LocalField = $value WHERE " . $this->Parent->Parent->PrimaryKeyName . " = " . $this->Parent->PrimaryKeyValue;
//		$out .= $SQL . "<BR>";
		$Query = $this->Parent->Parent->DB->CreateQuery($SQL);
		$count = $Query->AffectedRows();
		$Query->Close();
		return $count;
	}

	/**
	* In view mode this control will render the text of ForeignDataField.  In edit mode, it displays a drop down list box.
	* @todo add QDT_ in front of all request variable names.
	* @param array $DataRow
	* @param integer $DisplayMode QD_MODE_VIEW, QD_MODE_EDIT, QD_MODE_ADD
	*/
	function Render($DataRow, $DisplayMode = QD_MODE_VIEW)
	{
		$DB = $this->Parent->Parent->DB;
		if ($DisplayMode != QD_MODE_ADD)
		{
			$this->Value = $DataRow[$this->LocalField];
		}
		else
		{
			$this->Value = $this->DefaultValue;
		}
	
		// Render the Caption
		$out .= "\t<tr>\n\t\t<td class=\"" . $this->Parent->ViewCaptionCssClassName . "\">";
		$out .= $this->Caption . "</td>\n";
		$out .= "\t\t<td class=\"";
		$out .= $this->Parent->ViewCssClassName . "\">\n";
		
		if ($DisplayMode == QD_MODE_EDIT || $DisplayMode == QD_MODE_ADD  || $DisplayMode == QD_MODE_VIEW )
		{
			// Build the query.
			$SQL = "SELECT $this->ForeignKeyField, $this->ForeignDataField FROM $this->ForeignTable ";
			if ($DisplayMode == QD_MODE_VIEW || !$this->CanAdd || !$this->CanEdit)
			{  // if we're just viewing, grab the value
				if(!is_null($this->Value)) $SQL .= "WHERE $this->ForeignKeyField = {$this->Value}";
			}
			else
			{ // we're editing, so see if we need to filter by parentkey
				if (!is_null($this->ParentKeyName) && !is_null($this->ParentKeyValue))
				{
					$SQL .= stristr($SQL, "WHERE") ? "AND " : "WHERE ";
					$SQL .= "{$this->ParentKeyName} = {$this->ParentKeyValue} ";
	
				}
				$SQL .= "ORDER BY {$this->ForeignDataField}";
			}
//			$out .= $SQL . "<br />";
//			Execute the query
			$Query = $DB->CreateQuery($SQL);
		}

		if (($DisplayMode == QD_MODE_EDIT && $this->CanEdit ) || ($DisplayMode == QD_MODE_ADD  && $this->CanAdd) )
		{
			$out .= "\t\t<select class=\"" . $this->Parent->EditCssClassName . "\" name=\"$this->LocalField\">\n";
			if ( $this->IncludeNone ) // lets make the first one null
			{
				$out .= "\t\t\t<option value=\"\">None</option>\n";
			}
			while ($resultsRow = $Query->FetchRow())
			{
				$out .= "\t\t\t<option ";
				if ($this->Value == $resultsRow[$this->ForeignKeyField])
				{
					$out .= "selected ";
				}
				$out .= " value=\"" . $resultsRow[$this->ForeignKeyField] . "\">" . $resultsRow[$this->ForeignDataField] . "</option>\n";
			}
			$out .= "\t\t</select>";
		}
		elseif (!is_null($this->Value))
		{
			if ($DisplayMode == QD_MODE_SEARCH)
			{
				$out .= "\n\t\t<input type=\"text\" name=\"{$this->LocalField}\" class=\"{$this->Parent->EditCssClassName}\"  value=\"\">";
			}
			else
			{
				$resultsRow = $Query->FetchRow();
				$out .= $resultsRow[$this->ForeignDataField] . "\n\t\t<input type=\"hidden\" name=\"{$this->LocalField}\" value=\"{$this->Value}\">";
			}
		}
		$Query->Close();
		$out .= "</td>\n\t</tr>\n";
		return $out;

	}
}

?>